<?php
namespace ZHMVC\DB\MANAGER;

if (! isset($_SESSION)) {
    session_start();
}
include (dirname(dirname(__FILE__)) . "/zhconfig/Config.php");
include (ZH_PATH . DS . MANAGERFPATH . DS . 'islogin' . ZH);

$isp = new \ZHMVC\D\MANAGER\isPermission();
$isper = $isp->getPermission();
$_curlid = $isp->getCUrl();
$c = new \ZHCONFIG\ZhConfig();
$db_pre = $c->getDbPre();
if ($isper == 1) {
    $ErrMsg = "对不起，你没有访问该页面的权限";
    echo $ErrMsg;
    exit();
} elseif ($isper == 0) {
    $ErrMsg = "对不起，地址错误";
    echo $ErrMsg;
    exit();
}

include (ZH_PATH . DS . MANAGERFPATH . DS . 'top1' . ZH);
?>
<div class="wap-container">
	<nav class="breadcrumb" style="background-color: #fff; padding: 0 24px">
		模块商城管理 <a class="btn btn-success radius f-r"
			style="line-height: 1.6em; margin-top: 3px"
			href="javascript:location.replace(location.href);" title="刷新"><i
			class="Hui-iconfont">&#xe68f;</i></a>
	</nav>
<?php
$action = SafeRequest(getPGC('atcion'), 0);
switch ($action) {
    case "down":
        down($db_pre);
        break;
    case "down1":
        down1($db_pre);
        break;
    default:
        main($db_pre);
}

function main($db_pre)
{
    
    
?>
<article class="Hui-admin-content clearfix">
<div class="panel mt-20">
	<div class="panel-body">
		<div id="tab-system" class="HuiTab">
        	<div class="tabBar cl">
            	<span>模块</span>
            	<span>皮肤</span>
        	</div>
        	<div class="tabCon">
			<div class="clearfix mt-20">
			<table class="table table-border table-bordered table-hover table-bg table-sort">
				<thead>
					<tr class="text-c">
						<th>类型</th>
						<th>模块标识</th>
						<th>模块名称</th>
						<th>模块版本</th>
						<th>模块介绍</th>
						<th>前置条件</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
<?php
$json=httpRequest('https://mall.70ba.top/mall.php', null, 'GET');
$datas=json_decode($json,true);
//print_r($datas);
$rows=count($datas['data']);
for($i=0;$i<$rows;$i++)
{
    $data=$datas['data'][$i];
?>
               <tr class="text-c">
						<td><?php echo $data['moduletype']; ?></td>
						<td><?php echo $data['modulename']; ?></td>
						<td><?php echo $data['name']; ?></td>
						<td><?php echo $data['info']; ?></td>
						<td><?php echo $data['version']; ?></td>
						<td><?php  
						if($data['condition']=="") echo "无";
						else echo $data['condition']; ?></td>
					 	<td class="taskOptions">
<?php 
$module_conn=new \ZHMVC\D\Module();
$module_data=$module_conn->getOne1("modulename='".$data['modulename']."'");
$module_rows=$module_conn->getRows();
if($module_rows>0)
{
    echo "<span class=\"c-warning\">已安装</span>";
}
else 
{
?>
					 	
            				<a href="?atcion=down&modulename=<?php echo $data['modulename'];?>&modulepath=<?php echo $data['modulepath'];?>&prefix=<?php echo $data['prefix'];?>&name=<?php echo $data['name'];?>&downpath=<?php echo $data['downpath'];?>&version=<?php echo $data['version'];?>&condition=<?php echo $data['condition'];?>">安装</a>
<?php 
}
?>
						</td> 
					</tr>
<?php
}
?>
                 </tbody>
			</table>
			</div>
			</div>	
			
			<div class="tabCon">
			<div class="clearfix mt-20">
			<table class="table table-border table-bordered table-hover table-bg table-sort">
				<thead>
					<tr class="text-c">
						<th>类型</th>
						<th>皮肤标识</th>
						<th>皮肤名称</th>
						<th>皮肤版本</th>
						<th>皮肤介绍</th>
						<th>前置条件</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
<?php
$json=httpRequest('https://mall.70ba.top/mall.php', null, 'GET');
$datas=json_decode($json,true);
//print_r($datas);
$rows=count($datas['data']);
for($i=0;$i<$rows;$i++)
{
    $data=$datas['data'][$i];
?>
               <tr class="text-c">
						<td><?php echo $data['moduletype']; ?></td>
						<td><?php echo $data['modulename']; ?></td>
						<td><?php echo $data['name']; ?></td>
						<td><?php echo $data['info']; ?></td>
						<td><?php echo $data['version']; ?></td>
						<td><?php  
						if($data['condition']=="") echo "无";
						else echo $data['condition']; ?></td>
					 	<td class="taskOptions">
<?php 
$module_conn=new \ZHMVC\D\Module();
$module_data=$module_conn->getOne1("modulename='".$data['modulename']."'");
$module_rows=$module_conn->getRows();
if($module_rows>0)
{
    echo "<span class=\"c-warning\">已安装</span>";
}
else 
{
?>
					 	
            				<a href="?atcion=down1&modulename=<?php echo $data['modulename'];?>&modulepath=<?php echo $data['modulepath'];?>&prefix=<?php echo $data['prefix'];?>&name=<?php echo $data['name'];?>&downpath=<?php echo $data['downpath'];?>&version=<?php echo $data['version'];?>&condition=<?php echo $data['condition'];?>">安装</a>
<?php 
}
?>
						</td> 
					</tr>
<?php
}
?>
                 </tbody>
			</table>
			</div>
			</div>
			
			
		</div>			
					
				</div>
			</div>
</article>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
<!--/请在上方写此页面业务相关的脚本-->
<script type="text/javascript">
$(function(){
	$("#tab-system").Huitab();
});
</script>
</body>
</html>
<?php
}

function down(){
    
    $downpath = SafeRequest(getPGC('downpath'), 0);
    $modulepath = SafeRequest(getPGC('modulepath'), 0);
    $name = SafeRequest(getPGC('name'), 0);
    $prefix = SafeRequest(getPGC('prefix'), 0);
    $modulename = SafeRequest(getPGC('modulename'), 0);
    $version = SafeRequest(getPGC('version'), 0);
    $condition= SafeRequest(getPGC('condition'), 0);
    
    //判断是否有前置模块需要安装
    if($condition!="")
    {
        $condition_arr=explode(",", $condition);
        for($i=0;$i<count($condition_arr);$i++)
        {
            $condition_arrA=explode("=", $condition_arr[$i]);
            
            $module_conn=new \ZHMVC\D\Module();
            $module_data=$module_conn->getOne1("modulename='".$condition_arrA[0]."'");
            $module_rows=$module_conn->getRows();
            if($module_rows>0)
            {
                
            }
            else 
            {
                echo "需要先安装" . $condition_arrA[1] . "模块";
                exit();
            }
        }
    }
    
    $localpath=DownUpdateFile($downpath,$modulepath);
    
    echo "1.下载完成......";
    $fileAddess = ZH_PATH.$localpath;
    $toDir = ZH_PATH."/".$modulepath;
    if (! file_exists($toDir)) {
        $old = umask(0);
        mkdir($toDir, 0775);
        umask($old);
    }
    $zipFile = new \PhpZip\ZipFile();
    $zipFile->openFile($fileAddess)->extractTo($toDir);
    $zipFile->close();
    echo "2.解压完成......";
    
    $mulu = $modulepath;
    $biaoqianzhui = $prefix;
    $rpctype = '本地';
    $rpcurl = '';
    $rpcmainkey = '';
    $rpchost = '';
    $rpcprivatekey = '';
    $rpcmoduleid = '1';
    
    if (is_dir(ZH_PATH . DS . $mulu) == false) {
        echo "" . $mulu . "不是一个有效的目录";
        exit();
    }
    
    if (is_dir(ZH_PATH . DS . $mulu . DS . "install") == false) {
        echo "" . $mulu . "中安装目录不存在";
        exit();
    }
    
    if (is_file(ZH_PATH . DS . $mulu . DS . "install" . DS . "install.php") == false) {
        echo "" . $name . "安装文件不存在";
        exit();
    }
    
    if (is_file(ZH_PATH . DS . $mulu . DS . "install" . DS . "flock.php") == true) {
        echo "" . $name . "已经安装过了";
        exit();
    }
    echo '<iframe width="100%" height="400" frameborder="0" name="i" src="/' . $mulu . '/install/install.php?qianzui=' . $biaoqianzhui . '&mulu=' . $mulu . '&name=' . $name . '&modulename=' . $modulename . '&rpctype=' . $rpctype . '&rpcurl=' . $rpcurl . '&rpcmainkey=' . $rpcmainkey . '&rpchost=' . $rpchost . '&rpcprivatekey=' . $rpcprivatekey . '&rpcmoduleid=' . $rpcmoduleid . '&version=' . $version . '"></iframe>';
?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
<?php
}

function down1(){
    
    $downpath = SafeRequest(getPGC('downpath'), 0);
    $modulepath = SafeRequest(getPGC('modulepath'), 0);
    $name = SafeRequest(getPGC('name'), 0);
    $prefix = SafeRequest(getPGC('prefix'), 0);
    $modulename = SafeRequest(getPGC('modulename'), 0);
    $version = SafeRequest(getPGC('version'), 0);
    $condition= SafeRequest(getPGC('condition'), 0);
    
    //判断是否有前置模块需要安装
    if($condition!="")
    {
        $condition_arr=explode(",", $condition);
        for($i=0;$i<count($condition_arr);$i++)
        {
            $condition_arrA=explode("=", $condition_arr[$i]);
            
            $module_conn=new \ZHMVC\D\Module();
            $module_data=$module_conn->getOne1("modulename='".$condition_arrA[0]."'");
            $module_rows=$module_conn->getRows();
            if($module_rows>0)
            {
                
            }
            else
            {
                echo "需要先安装" . $condition_arrA[1] . "模块";
                exit();
            }
        }
    }
    
    $localpath=DownUpdateFile($downpath,$modulepath);
    
    echo "1.下载完成......";
    $fileAddess = ZH_PATH.$localpath;
    $toDir = ZH_PATH."/".$modulepath;
    if (! file_exists($toDir)) {
        $old = umask(0);
        mkdir($toDir, 0775);
        umask($old);
    }
    $zipFile = new \PhpZip\ZipFile();
    $zipFile->openFile($fileAddess)->extractTo($toDir);
    $zipFile->close();
    echo "2.解压完成......";
    
    $mulu = $modulepath;
    $biaoqianzhui = $prefix;
    $rpctype = '本地';
    $rpcurl = '';
    $rpcmainkey = '';
    $rpchost = '';
    $rpcprivatekey = '';
    $rpcmoduleid = '1';
    
    if (is_dir(ZH_PATH . DS . $mulu) == false) {
        echo "" . $mulu . "不是一个有效的目录";
        exit();
    }
    
    if (is_dir(ZH_PATH . DS . $mulu . DS . "install") == false) {
        echo "" . $mulu . "中安装目录不存在";
        exit();
    }
    
    if (is_file(ZH_PATH . DS . $mulu . DS . "install" . DS . "install.php") == false) {
        echo "" . $name . "安装文件不存在";
        exit();
    }
    
    if (is_file(ZH_PATH . DS . $mulu . DS . "install" . DS . "flock.php") == true) {
        echo "" . $name . "已经安装过了";
        exit();
    }
    echo '<iframe width="100%" height="400" frameborder="0" name="i" src="/' . $mulu . '/install/install.php?qianzui=' . $biaoqianzhui . '&mulu=' . $mulu . '&name=' . $name . '&modulename=' . $modulename . '&rpctype=' . $rpctype . '&rpcurl=' . $rpcurl . '&rpcmainkey=' . $rpcmainkey . '&rpchost=' . $rpchost . '&rpcprivatekey=' . $rpcprivatekey . '&rpcmoduleid=' . $rpcmoduleid . '&version=' . $version . '"></iframe>';
    ?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
<?php
}


